﻿using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using CCSManager.CCS;

namespace CCSManager
{
    /// <summary>
    /// Interaction logic for Product.xaml
    /// </summary>
    public partial class Product : UserControl
    {
        public Product()
        {
            InitializeComponent();
        }

        public DeveloperApplication Application { get; set; }

        public event EventHandler EditCancelled;

        public event EventHandler<ProductDeleteRequestedEventArgs> ProductDeleteRequested;

        public event EventHandler<ProductUpdateRequestedEventArgs> ProductUpdateRequested;

        public void Refresh()
        {
            if (Application != null)
            {
                appNameTextBox.Text = Application.Name;
                majorVersionTextBox.Text = Application.Version[0].ToString();
                minorVersionTextBox.Text = Application.Version[1].ToString();
                buildTextBox.Text = Application.Version[2].ToString();
                revisionTextBox.Text = Application.Version[3].ToString();
                licensesTextBox.Text = Application.LicensesPerInvoice.ToString();
                trialTextBox.Text = Application.TrialLengthInDays.ToString();

                issuesDataGrid.ItemsSource = this.Application.Issues;
                exceptionsDataGrid.ItemsSource = this.Application.Exceptions;
                registrationsDataGrid.ItemsSource = this.Application.Registrations;
            }
        }

        private void backButton_Click(object sender, RoutedEventArgs e)
        {
            _RaiseEditCancelled();
        }

        private void _RaiseEditCancelled()
        {
            if (EditCancelled != null)
            {
                EditCancelled(this, new EventArgs());
            }
        }

        public void Close()
        {
            _RaiseEditCancelled();
        }

        private void clipboardButton_Click(object sender, RoutedEventArgs e)
        {
            const string devTokenXmlFormat = "<Setting Name=\"DevToken\" Type=\"System.String\" Scope=\"Application\"><Value Profile=\"(Default)\">{0}</Value></Setting>";
            const string productNameXmlFormat = "<Setting Name=\"ProductName\" Type=\"System.String\" Scope=\"Application\"><Value Profile=\"(Default)\">{0}</Value></Setting>";
            const string trialLengthXmlFormat = "<Setting Name=\"TrialLength\" Type=\"System.Int32\" Scope=\"Application\"><Value Profile=\"(Default)\">{0}</Value></Setting>";

            StringBuilder settingsString = new StringBuilder();
            settingsString.AppendFormat(devTokenXmlFormat, App.DeveloperToken.ToString().ToUpperInvariant());
            settingsString.AppendFormat(productNameXmlFormat, this.Application.Name);
            settingsString.AppendFormat(trialLengthXmlFormat, this.Application.TrialLengthInDays);

            Clipboard.SetText(settingsString.ToString());
        }

        private void updateButton_Click(object sender, RoutedEventArgs e)
        {
            if (ProductUpdateRequested != null)
            {
                Application.Name = appNameTextBox.Text;
                Application.Version = new[] { int.Parse(majorVersionTextBox.Text), int.Parse(minorVersionTextBox.Text), int.Parse(buildTextBox.Text), int.Parse(revisionTextBox.Text) };
                Application.LicensesPerInvoice = int.Parse(licensesTextBox.Text);
                Application.TrialLengthInDays = int.Parse(trialTextBox.Text);

                ProductUpdateRequested(this, new ProductUpdateRequestedEventArgs(this.Application));
            }
        }

        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            if (ProductDeleteRequested != null)
            {
                ProductDeleteRequested(this, new ProductDeleteRequestedEventArgs(Application));
            }

        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            _RaiseEditCancelled();
        }
    }

    public class ProductDeleteRequestedEventArgs : EventArgs
    {
        public ProductDeleteRequestedEventArgs(DeveloperApplication application)
        {
            Application = application;
        }

        public DeveloperApplication Application { get; private set; }
    }

    public class ProductUpdateRequestedEventArgs : EventArgs
    {
        public ProductUpdateRequestedEventArgs(DeveloperApplication application)
        {
            Application = application;
        }

        public DeveloperApplication Application { get; private set; }
    }
}